package com.cogent.business.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cogent.business.domain.WebTitleDomain;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessQueryHelper;

public class WebBusHelper {
	private final static String TITLE_LIMIT_SPAN = "..."; 
	private final static String ROOT_ARCTILE_VIEW = "Web_view.action?id="; 
	private static HashMap IMAGE_NAME_MAP = new HashMap();
	private static HashMap PARENT_NAME_MAP = new HashMap();
	
	static{
		IMAGE_NAME_MAP.put("XJSHJDX", "lefttitle1.jpg");
		IMAGE_NAME_MAP.put("ZHBQK", "lefttitle1.jpg");
		IMAGE_NAME_MAP.put("GZHDT", "lefttitle1.jpg");
		IMAGE_NAME_MAP.put("DYFW", "lefttitle2.jpg");
		IMAGE_NAME_MAP.put("SHQFW", "lefttitle2.jpg");
		IMAGE_NAME_MAP.put("DWZHSH", "lefttitle5.jpg");
		IMAGE_NAME_MAP.put("ZHTXX", "lefttitle5.jpg");
		IMAGE_NAME_MAP.put("XDTH", "lefttitle5.jpg");
		IMAGE_NAME_MAP.put("TZHGG", "lefttitle6.jpg");
		IMAGE_NAME_MAP.put("DZHZHGK", "lefttitle1.jpg");
		IMAGE_NAME_MAP.put("QUHPYJN", "lefttitle3.jpg");
		IMAGE_NAME_MAP.put("QUHZXLY", "lefttitle3.jpg");
		IMAGE_NAME_MAP.put("QUHDJGZ", "lefttitle4.jpg");
		IMAGE_NAME_MAP.put("QUHSHQJS", "lefttitle4.jpg");
		IMAGE_NAME_MAP.put("QYHDJFC", "qyhdj.jpg");
		
		PARENT_NAME_MAP.put("XJSHJDX", "支部风采");
		PARENT_NAME_MAP.put("ZHBQK", "支部风采");
		PARENT_NAME_MAP.put("GZHDT", "支部风采");
		PARENT_NAME_MAP.put("DYFW", "服务之窗");
		PARENT_NAME_MAP.put("SHQFW", "服务之窗");
		PARENT_NAME_MAP.put("DWZHSH", "学习园地");
		PARENT_NAME_MAP.put("ZHTXX", "学习园地");
		PARENT_NAME_MAP.put("XDTH", "学习园地");
		PARENT_NAME_MAP.put("TZHGG", "通知公告");
		PARENT_NAME_MAP.put("DZHZHGK", "支部风采");
		PARENT_NAME_MAP.put("QUHPYJN", "交流互动");
		PARENT_NAME_MAP.put("QUHZXLY", "交流互动");
		PARENT_NAME_MAP.put("QUHDJGZ", "建言献策");
		PARENT_NAME_MAP.put("QUHSHQJS", "建言献策");
		PARENT_NAME_MAP.put("QYHDJFC", "区域化党建-五进社区");
	}
	
	public static HashMap getIndexPic(HttpServletRequest request, int num, int maxLength) {
		String sql = "select * from (select imgsrc,title,id,menutype from web_newscontent where status=0 and imgsrc is not null order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listToHashMap(list, maxLength);
	}
	
	public static HashMap getArctilePic(HttpServletRequest request, String catid, int num, int maxLength) {
		String sql = "select * from (select imgsrc,title,id,menutype from web_newscontent where status=0 and menutype=" + catid + " and imgsrc is not null order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listToHashMap(list, maxLength);
	}
	
	public static HashMap getArctilePicArray(HttpServletRequest request, String catid, int num, int maxLength) {
		String sql = "select * from (select imgsrc,title,id,menutype from web_newscontent where status=0 and menutype in(" + catid + ") and imgsrc is not null order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listToHashMap(list, maxLength);
	}
	
	public static HashMap getStudyArctilePic(HttpServletRequest request, String catid, int num, int maxLength) {
		String sql = "select * from (select imgsrc,title,id,menutype from web_studycontent where status=0 and menutype=" + catid + " and imgsrc is not null order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_studycontent", sql);
		
		return listToHashMap(list, maxLength);
	}
	
	public static HashMap getArctileContentPic(HttpServletRequest request, String catid, int num, int maxLength, int contentLen) {
		String sql = "select * from (select imgsrc,title,content,id from web_newscontent where status=0 and menutype=" + catid + " and imgsrc is not null order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listContentToHashMap(list, maxLength, contentLen);
	}
	
	public static List getArctileTitile(String code, int num, int maxeLength) {
		String sql = "select * from (select title, pubdate,id from web_newscontent where status=0 and menutype=" + code + " order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listToWebDomianList(list, maxeLength);
	}
	
	public static List getArctileTitileArray(String code, int num, int maxeLength) {
		String sql = "select * from (select title, pubdate,id from web_newscontent where status=0 and menutype in(" + code + ") order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_newscontent", sql);
		
		return listToWebDomianList(list, maxeLength);
	}
	
	public static List getStudyArctileTitile(String code, int num, int maxeLength) {
		String sql = "select * from (select title, pubdate,id from web_studycontent where status=0 and menutype=" + code + " order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_studycontent", sql);
		
		return listToWebDomianList(list, maxeLength);
	}
	
	public static List getStudyIsheedTitile(String code, int num, int maxeLength) {
		String sql = "select * from (select title, pubdate,id,menutype from web_studycontent where status=0 and isneed="+code+" order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_studycontent", sql);
		
		return listToWebDomianList(list, maxeLength);
	}
	
	
	private static List listToWebDomianList(List list, int maxeLength) {
		List retList = new ArrayList();
		Iterator it = list.iterator();
		while(it.hasNext()) {
			HashMap temp = (HashMap)it.next();
			WebTitleDomain webdomain = new WebTitleDomain();
			webdomain.setId(MapUtil.getStringValue(temp, "id"));
			webdomain.setPublishdate(MapUtil.getStringValue(temp, "pubdate"));
			webdomain.setTitle(getTitle(MapUtil.getStringValue(temp, "title"), maxeLength));
			webdomain.setMenutype(MapUtil.getStringValue(temp, "menutype")); 
			retList.add(webdomain);
		}
		
		return retList;
	}
	
	public static List listLink() {
		String sql = "select * from web_butmlink where status=0 order by sequence desc";
		List list = BussinessQueryHelper.query("web_butmlink", sql);
		
		return list;
	}
	
	public static List listTopLink(int num) {
		String sql = "select * from (select * from web_contentlink where status=0 order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query("web_contentlink", sql);
			
		return list;
	}
	
	public static String getImageName(String catid) {
		return MapUtil.getStringValue(IMAGE_NAME_MAP, catid);
	}
	
	public static String getParentCatName(String catid) {
		return MapUtil.getStringValue(PARENT_NAME_MAP, catid);
	}
	
	private static HashMap listToHashMap(List list, int maxLength) {
		HashMap curMap = new HashMap();
		Iterator it = list.iterator();
		int curNum = list.size();
		String pics = "";
		String title = "";
		String links = "";
		String span = "";
		int i = 1;
		while(it.hasNext()) {
			if(i < curNum)
				span = "|";
			HashMap temp = (HashMap)it.next();
			pics += MapUtil.getStringValue(temp, "imgsrc") + span;
			title += getTitle(MapUtil.getStringValue(temp, "title"), maxLength) + span;
			links += ROOT_ARCTILE_VIEW + MapUtil.getStringValue(temp, "id") + "&menutype=" + MapUtil.getStringValue(temp, "menutype") + span;
		}
		curMap.put("PICS", pics);
		curMap.put("TITILES", title);
		curMap.put("IDS", links);
		
		return curMap;
	}
	
	private static HashMap listContentToHashMap(List list, int maxLength, int contentLen) {
		HashMap curMap = new HashMap();
		Iterator it = list.iterator();
		int curNum = list.size();
		String pics = "";
		String title = "";
		String links = "";
		String span = "";
		String content = "";
		int i = 1;
		while(it.hasNext()) {
			if(i < curNum)
				span = "|";
			HashMap temp = (HashMap)it.next();
			pics += MapUtil.getStringValue(temp, "imgsrc") + span;
			title += getTitle(MapUtil.getStringValue(temp, "title"), maxLength) + span;
			links += ROOT_ARCTILE_VIEW + MapUtil.getStringValue(temp, "id") + span;
			content += getTitle(MapUtil.getStringValue(temp, "content"), contentLen) + span;
		}
		curMap.put("PICS", pics);
		curMap.put("TITILES", title);
		curMap.put("IDS", links);
		curMap.put("CONTENTS", content);
		
		return curMap;
	}
	
	private static String getTitle(String title, int maxLength) {
		String ret = "";
		if(title.length() < maxLength)
			ret = title;
		else
			ret = title.substring(0, maxLength) + TITLE_LIMIT_SPAN;
		
		return ret;
	}
	
	public static List getPlanList(String tableName, int num) {
		String sql = "select * from (select * from " + tableName + " where status=0 order by joindate desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query(tableName, sql);
		
		return list;
	}
	
	public static List getArctileTitile(String tableName, String code, int num, int maxeLength) {
		String sql = "select * from (select title, pubdate,id from " + tableName + " where status=0";
		if(!StringUtil.isEmpty(code))
			sql += " and menutype=" + code;
		sql += " order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query(tableName, sql);
		
		return listToWebDomianList(list, maxeLength);
	}
	
	public static List getMemberPhotoList(int size) {
		String sql = "select imgsrc, names from (select * from web_membercontent where status=0 order by modifydate desc) tem where rownum<=" + size;
		List list = BussinessQueryHelper.query("web_membercontent", sql);
		
		return list;
	}
	
	public static List getNetPhotoList(int size, int type) {
		String sql = "select imgsrc, names from (select * from web_photocontent where status=0 and menutype=" + type + " order by modifydate desc) tem where rownum<=" + size;
		List list = BussinessQueryHelper.query("web_photocontent", sql);
		
		return list;
	}
	
	public static List getNetPlanList(String tableName, int num, int type) {
		String sql = "select * from (select * from " + tableName + " where status=0 and menutype=" + type + " order by joindate desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query(tableName, sql);
		
		return list;
	}
	
	public static List getArticleInfo(String tableName, int num, int type) {
		String sql = "select * from (select * from " + tableName + " where status=0 and menutype=" + type + " order by sequence desc) tem where rownum<=" + num;
		List list = BussinessQueryHelper.query(tableName, sql);
		
		return list;
	}
}
